package com.thinkgem.jeesite.modules.sys.utils;

import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.modules.sys.entity.SysArea;
import com.thinkgem.jeesite.modules.sys.entity.SysOffice;
import com.thinkgem.jeesite.modules.sys.entity.SysUser;

import java.util.List;
import java.util.Map;

/**
 * @Author:Kongxiangsheng
 * @DesCription:
 * @Date:Created in 11:27 2018/8/27
 * @Modified By:
 */
public class SysCacheUtils extends CacheUtils {
    private static final String USER_CACHE = "userCache";
    private static final String USER_CACHE_ID_ = "userCache_id_";
    private static final String USER_CACHE_LOGIN_NAME_ = "userCache_loginName_";
    private static final String USER_CACHE_WECHAT_OPENID_ = "userCache_wechat_";
    private static final String OFFICE_CACHE = "officeCache";
    private static final String OFFICE_CACHE_ID_ = "officeCache_id_";
    private static final String AREA_CACHE = "areaCache";
    private static final String AREA_CACHE_ID_ = "areaCache_id_";
    private static final String CACHE_MENU_NAME_PATH_MAP = "menuNamePathMap";
    private static final String SYS_CACHE = "sysCache";
    private static final String LOGIN_FAIL_MAP = "loginFailMap";

    // ################# 用户缓存--begin##################

    /**
     * 从缓存中获取用户信息
     *
     * @param sysUserId
     * @return
     */
    public static SysUser getSysUserById(String sysUserId) {
        return (SysUser) getCache(USER_CACHE).get(getKey(USER_CACHE_ID_ + sysUserId));
    }

    /**
     * 保存用户信息到缓存中
     *
     * @param sysUserId
     * @param sysUser
     */
    public static void putSysUserById(String sysUserId, SysUser sysUser) {
        getCache(USER_CACHE).put(getKey(USER_CACHE_ID_ + sysUserId), sysUser);
    }

    /**
     * 删除某条用户缓存
     *
     * @param sysUserId
     */
    public static void removeSysUserById(String sysUserId) {

        getCache(USER_CACHE).remove(getKey(USER_CACHE_ID_ + sysUserId));
    }

    /**
     * 从缓存中获取用户信息
     *
     * @param loginName
     * @return
     */
    public static SysUser getSysUserByLoginName(String loginName) {
        return (SysUser) getCache(USER_CACHE).get(getKey(USER_CACHE_LOGIN_NAME_ + loginName));
    }

    /**
     * 保存用户信息到缓存中
     *
     * @param loginName
     * @param sysUser
     */
    public static void putSysUserByLoginName(String loginName, SysUser sysUser) {
        getCache(USER_CACHE).put(getKey(USER_CACHE_LOGIN_NAME_ + loginName), sysUser);
    }

    /**
     * 删除某条用户缓存
     *
     * @param loginName
     */
    public static void removeSysUserByLoginName(String loginName) {

        getCache(USER_CACHE).remove(getKey(USER_CACHE_LOGIN_NAME_ + loginName));
    }

    /**
     * 从缓存中获取用户信息
     *
     * @param wechatOpenid
     * @return
     */
    public static SysUser getSysUserByWechatOpenid(String wechatOpenid) {
        return (SysUser) getCache(USER_CACHE).get(getKey(USER_CACHE_WECHAT_OPENID_ + wechatOpenid));
    }

    /**
     * 删除所有的用户缓存
     */
    public static void removeSysUserAll() {
        removeAll(USER_CACHE);
    }

    // ################# 区域缓存--begin##################

    /**
     * 从缓存中获取区域信息
     *
     * @param sysAreaId
     * @return
     */
    public static SysArea getSysArea(String sysAreaId) {
        return (SysArea) getCache(AREA_CACHE).get(getKey(AREA_CACHE_ID_ + sysAreaId));
    }

    /**
     * 保存区域信息到缓存中
     *
     * @param sysAreaId
     * @param sysArea
     */
    public static void putSysArea(String sysAreaId, SysArea sysArea) {
        getCache(AREA_CACHE).put(getKey(AREA_CACHE_ID_ + sysAreaId), sysArea);
    }

    /**
     * 删除某条区域缓存
     *
     * @param sysAreaId
     */
    public static void removeSysArea(String sysAreaId) {

        getCache(AREA_CACHE).remove(getKey(AREA_CACHE_ID_ + sysAreaId));
    }

    /**
     * 删除所有的区域缓存
     */
    public static void removeSysAreaAll() {
        removeAll(AREA_CACHE);
    }

    // ################# 机构缓存--begin##################

    /**
     * 从缓存中获取机构信息
     *
     * @param sysOfficeId
     * @return
     */
    public static SysOffice getSysOffice(String sysOfficeId) {
        return (SysOffice) getCache(OFFICE_CACHE).get(getKey(OFFICE_CACHE_ID_ + sysOfficeId));
    }

    /**
     * 保存机构信息到缓存中
     *
     * @param sysOfficeId
     * @param sysOffice
     */
    public static void putSysOffice(String sysOfficeId, SysOffice sysOffice) {
        getCache(OFFICE_CACHE).put(getKey(OFFICE_CACHE_ID_ + sysOfficeId), sysOffice);
    }

    /**
     * 删除某条机构缓存
     *
     * @param sysOfficeId
     */
    public static void removeSysOffice(String sysOfficeId) {

        getCache(OFFICE_CACHE).remove(getKey(OFFICE_CACHE_ID_ + sysOfficeId));
    }

    /**
     * 删除所有的机构缓存
     */
    public static void removeSysOfficeAll() {
        removeAll(OFFICE_CACHE);
    }

 // ################# 菜单缓存--begin##################

    /**
     * 获取菜单全路径Map
     *
     * @return
     */
    public static Map<String, String> getMenuPathMap() {
        return (Map<String, String>) getCache(SYS_CACHE).get(getKey(CACHE_MENU_NAME_PATH_MAP));
    }

    /**
     * 保存菜单全路径Map
     *
     * @param menuPathMap
     */
    public static void setMenuPathMap(Map<String, String> menuPathMap) {
        getCache(SYS_CACHE).put(getKey(CACHE_MENU_NAME_PATH_MAP), menuPathMap);
    }

    public static void removeMenuPathMap() {
        getCache(SYS_CACHE).remove(getKey(CACHE_MENU_NAME_PATH_MAP));
    }

    // ################# 登录相关缓存--begin##################

    /**
     * @return
     */
    public static Map<String, Integer> getLoginFailMap() {
        return (Map<String, Integer>) getCache(SYS_CACHE).get(getKey(LOGIN_FAIL_MAP));
    }

    /**
     * @param loginFailMap
     */
    public static void putLoginFailMap(Map<String, Integer> loginFailMap) {
        getCache(SYS_CACHE).put(getKey(LOGIN_FAIL_MAP), loginFailMap);
    }
}
